import { _decorator, BoxCollider2D, CircleCollider2D, Component, Contact2DType, director, Node } from 'cc';
import { GameColliderTag, GameState } from './commons/GameConst';
import { GameEvent } from './commons/GameEvent';
import { GameData } from './commons/GameData';
const { ccclass, property } = _decorator;

@ccclass('BirdCollider')
export class BirdCollider extends Component {





    start() {

        let collider = this.node.getComponent(BoxCollider2D);
        if(collider){
            collider.on(Contact2DType.BEGIN_CONTACT/*碰撞开始*/, this.onBeginContact, this/*this指向当前对象*/);
        }
    }

    onBeginContact(selfCollider: CircleCollider2D, otherCollider: CircleCollider2D, contact: any) {
        if(selfCollider.tag == GameColliderTag.GCT_Bird && GameState.GS_GamePlaying == GameData.instance.curGameState){

                if(otherCollider.tag == GameColliderTag.GCT_PipeDown||
                   otherCollider.tag == GameColliderTag.GCT_PipeUp||
                   otherCollider.tag == GameColliderTag.GCT_Land){

                    director.emit(GameEvent.GAME_EVENT_END);
                   }else{

                   }
        }

        if(selfCollider.tag == GameColliderTag.GCT_Bird && otherCollider.tag == GameColliderTag.GCT_CheckScore){
            GameData.instance.curScore++;
            director.emit(GameEvent.GAME_EVENT_UPDATE_SCORE);
        }


    }

    update(deltaTime: number) {
        
    }
}


